#! /bin/bash
# 传入参数：install 表示更新项目并执行安装依赖操作

#信息日志
log_info() {
  content="[INFO] $(date '+%Y-%m-%d %H:%M:%S') $@"
  echo -e "\033[32m"${content}"\033[0m"
}

time_path=$(date "+%Y-%m-%d_%H:%M:%S")
Server_Project_Path='/home/workplace/ServerProject'
Express_Demo_Path='/home/workplace/ServerProject/expressDemo'
Log_File_Path="/home/logs/express-demo-$time_path.out"
time_str=$(date "+%Y-%m-%d %H:%M:%S")

# 输出时间标识
log_info "--------expressDemo start $time_str--------"

# 进入项目路径-拉取项目
cd $Server_Project_Path

git checkout .
git pull

# 将vue-front和vue-system项目下的dist目录迁移出来
rm -rf $Express_Demo_Path/static/chat/ && cp -r $Express_Demo_Path/vue-front/dist/build/h5/ $Express_Demo_Path/static/chat/

rm -rf $Express_Demo_Path/static/system/ && cp -r $Express_Demo_Path/vue-system/dist/ $Express_Demo_Path/static/system/

# 进入expressDemo目录-运行项目
cd $Express_Demo_Path

if [ "$1" = install ]; then
  echo "传入参数install"
  npm install
fi

express_demo_pid=$(netstat -tunlp | grep 3000 | awk '{split($7,arr,"/");print arr[1]}')
if [ ! $express_demo_pid ]; then
  log_info "express_demo_pid 不存在，不需要kill"
else
  kill $express_demo_pid
  log_info "kill express_demo_pid $express_demo_pid success"
fi

nohup npm run serve:product >$Log_File_Path 2>&1 &
end_time_str=$(date "+%Y-%m-%d %H:%M:%S")

log_info "==========expressDemo start success $end_time_str=========="
